<?php
class CategoriesController extends StoreAppController {

	var $name = 'StoreCategories';
	var $permissions = array(
		'add' => array('admin','manag'),
		'delete' => array('admin','manag'),
		'down' => array('admin','manag'),
		'edit' => array('admin','manag'),
		'index' => array('admin','manag'),
		'up' => array('admin','manag'),
		'view' => array('admin','manag')
	);
	public function beforeFilter(){
		parent::beforeFilter();
//		$this->layout = 'cms';
	}
	
	function index() {
		$this->StoreCategory->recursive = 0;
		$this->paginate = array('order'=>array("lft"=>"asc"));
		$categories = $this->treeForCombobox( $this->paginate(),'StoreCategory',true);
		$this->set('storeCategories',$categories );
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__d('store','Invalid store category', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('storeCategory', $this->StoreCategory->read(null, $id));
	}

	function add($parent_id=null) {
		if (!empty($this->data)) {
			$this->StoreCategory->create();
			if ($this->StoreCategory->save($this->data)) {
				$this->Session->setFlash(__d('store','The store category has been saved', true));				
				$this->redirect(array('action' => 'add',$this->data['StoreCategory']['parent_id']));
			} else {
				$this->Session->setFlash(__d('store','The store category could not be saved. Please, try again.', true));
			}
		}
		
		$parentCategory = $this->StoreCategory->find('threaded', array());
		$parentCategories = $this->treeForCombobox($parentCategory,'StoreCategory');
		$this->data['StoreCategory']['parent_id'] = (is_null($parent_id)) ? null : $parent_id;
		$this->set(compact('parentCategories'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__d('store','Invalid store category', true));
			$this->redirect($this->referer());
		}
		if (!empty($this->data)) {
			if ($this->StoreCategory->save($this->data)) {
				// parent_id musi byc 0
				$this->StoreCategory->updateAll(
				    array('StoreCategory.parent_id' => 0),
				    array('StoreCategory.parent_id' => NULL)
				);
				
				$this->Session->setFlash(__d('store','The store category has been saved', true));
				$this->redirect($this->referer());
			} else {
				$this->Session->setFlash(__d('store','The store category could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {			
			$this->data = $this->StoreCategory->read(null, $id);
		}
		$parentCategory = $this->StoreCategory->find('threaded', array());
		$parentCategories = $this->treeForCombobox($parentCategory,'StoreCategory');			
		$this->set(compact('parentCategories'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__d('store','Invalid id for store category', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->StoreCategory->delete($id)) {
			$this->Session->setFlash(__d('store','Store category deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__d('store','Store category was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	/**
	 * 
	 * Przesun do góry
	 * @param $id
	 * @param $delta
	 */
	public function up($id = null, $delta = null){
		$cat = $this->StoreCategory->findById($id);
        if (empty($cat)) {
            $this->Session->setFlash(__d("store","There is no category.",true) );
            $this->redirect($this->referer());
        }        
        $this->StoreCategory->id = $cat['StoreCategory']['id'];
        
        if ($delta > 0) {  
            $this->StoreCategory->moveup($this->StoreCategory->id, abs($delta));
        } else {
            $this->Session->setFlash(__d("store","Please provide a number of positions the category should be moved up.",true)); 
        }    
        $this->redirect($this->referer());    
    }
    
    /**
     * 
     * przenies na dol
     * @param Numeric $id
     * @param Numeric $delta
     */
	public function down($id = null, $delta = null) {
		$cat = $this->StoreCategory->findById($id);
        if (empty($cat)) {
            $this->Session->setFlash(__d("store","There is no category",true));
            $this->redirect($this->referer());
        }        
        $this->StoreCategory->id = $cat['StoreCategory']['id'];        
        if ($delta > 0) {  
            $this->StoreCategory->moveDown($this->StoreCategory->id, abs($delta));
        } else {
            $this->Session->setFlash(__d("store","Please provide the number of positions the field should be moved down.",true)); 
        }    
        $this->redirect($this->referer());
    }	
}
?>